package com.lsyonlygoddes.controller;

import com.lsyonlygoddes.vo.FileVo;
import com.lsyonlygoddes.util.FileUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@CrossOrigin
@RestController
public class FileController {
    @GetMapping("/fileList/{pageNum}/{pageSize}")
    public Map<String, Object> fileList(@RequestParam("parenPath") String parenPath,
                                        @PathVariable("pageNum")Integer pageNum,
                                        @PathVariable("pageSize")Integer pageSize
                                            ){
        Map<String, Object> map = new HashMap<>();
        List<FileVo> fileList = FileUtils.fileList(parenPath);

        map.put("total", fileList.size());
        //开始索引和结束索引
        int pageIndex = (pageNum - 1) * pageSize;
        int pageEnd = pageNum * pageSize;
//        索引越界处理
        if (pageEnd > fileList.size()){
            pageEnd = fileList.size();
        }
        fileList = fileList.subList(pageIndex, pageEnd);

        map.put("row", fileList);
        map.put("code", 200);

        return map;
    }


}
